home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch10 / RndTree.frm (.txt) < prev    next >
Visual Basic Form  |  1999-06-08  |  9KB  |  281 lines

  1. VERSION 5.00
  2. Begin VB.Form frmRndTree 
  3.    Caption         =   "RndTree"
  4.    ClientHeight    =   4050
  5.    ClientLeft      =   1140
  6.    ClientTop       =   1050
  7.    ClientWidth     =   7470
  8.    LinkTopic       =   "Form1"
  9.    PaletteMode     =   1  'UseZOrder
  10.    ScaleHeight     =   4050
  11.    ScaleWidth      =   7470
  12.    Begin VB.TextBox txtRndDTheta 
  13.       Height          =   285
  14.       Left            =   1320
  15.       MaxLength       =   3
  16.       TabIndex        =   14
  17.       Text            =   "10"
  18.       Top             =   1800
  19.       Width           =   615
  20.    End
  21.    Begin VB.CheckBox chkBend 
  22.       Caption         =   "Bend Branches"
  23.       Height          =   255
  24.       Left            =   240
  25.       TabIndex        =   13
  26.       Top             =   2640
  27.       Width           =   1455
  28.    End
  29.    Begin VB.TextBox txtMaxBranches 
  30.       Height          =   285
  31.       Left            =   1320
  32.       MaxLength       =   3
  33.       TabIndex        =   11
  34.       Text            =   "3"
  35.       Top             =   360
  36.       Width           =   615
  37.    End
  38.    Begin VB.TextBox txtRndScale 
  39.       Height          =   285
  40.       Left            =   1320
  41.       MaxLength       =   5
  42.       TabIndex        =   9
  43.       Text            =   "0.20"
  44.       Top             =   1080
  45.       Width           =   615
  46.    End
  47.    Begin VB.TextBox txtDTheta 
  48.       Height          =   285
  49.       Left            =   1320
  50.       MaxLength       =   3
  51.       TabIndex        =   2
  52.       Text            =   "36"
  53.       Top             =   1440
  54.       Width           =   615
  55.    End
  56.    Begin VB.TextBox txtLengthScale 
  57.       Height          =   285
  58.       Left            =   1320
  59.       MaxLength       =   5
  60.       TabIndex        =   1
  61.       Text            =   "0.75"
  62.       Top             =   720
  63.       Width           =   615
  64.    End
  65.    Begin VB.TextBox txtDepth 
  66.       Height          =   285
  67.       Left            =   1320
  68.       MaxLength       =   3
  69.       TabIndex        =   0
  70.       Text            =   "5"
  71.       Top             =   0
  72.       Width           =   615
  73.    End
  74.    Begin VB.CheckBox chkTaper 
  75.       Caption         =   "Taper Branches"
  76.       Height          =   255
  77.       Left            =   240
  78.       TabIndex        =   3
  79.       Top             =   2280
  80.       Width           =   1455
  81.    End
  82.    Begin VB.PictureBox picCanvas 
  83.       AutoRedraw      =   -1  'True
  84.       Height          =   4335
  85.       Left            =   2040
  86.       ScaleHeight     =   285
  87.       ScaleMode       =   3  'Pixel
  88.       ScaleWidth      =   357
  89.       TabIndex        =   6
  90.       Top             =   0
  91.       Width           =   5415
  92.    End
  93.    Begin VB.CommandButton cmdGo 
  94.       Caption         =   "Go"
  95.       Default         =   -1  'True
  96.       Height          =   375
  97.       Left            =   720
  98.       TabIndex        =   4
  99.       Top             =   3120
  100.       Width           =   615
  101.    End
  102.    Begin VB.Label Label1 
  103.       Caption         =   "Rnd DTheta"
  104.       Height          =   255
  105.       Index           =   3
  106.       Left            =   0
  107.       TabIndex        =   15
  108.       Top             =   1800
  109.       Width           =   1335
  110.    End
  111.    Begin VB.Label Label1 
  112.       Caption         =   "Max Branches"
  113.       Height          =   255
  114.       Index           =   5
  115.       Left            =   0
  116.       TabIndex        =   12
  117.       Top             =   360
  118.       Width           =   1335
  119.    End
  120.    Begin VB.Label Label1 
  121.       Caption         =   "Rnd Scale"
  122.       Height          =   255
  123.       Index           =   4
  124.       Left            =   0
  125.       TabIndex        =   10
  126.       Top             =   1080
  127.       Width           =   1335
  128.    End
  129.    Begin VB.Label Label1 
  130.       Caption         =   "DTHETA"
  131.       Height          =   255
  132.       Index           =   2
  133.       Left            =   0
  134.       TabIndex        =   8
  135.       Top             =   1440
  136.       Width           =   1335
  137.    End
  138.    Begin VB.Label Label1 
  139.       Caption         =   "LENGTH_SCALE"
  140.       Height          =   255
  141.       Index           =   1
  142.       Left            =   0
  143.       TabIndex        =   7
  144.       Top             =   720
  145.       Width           =   1335
  146.    End
  147.    Begin VB.Label Depth 
  148.       Caption         =   "Level"
  149.       Height          =   255
  150.       Index           =   0
  151.       Left            =   0
  152.       TabIndex        =   5
  153.       Top             =   0
  154.       Width           =   1335
  155.    End
  156. Attribute VB_Name = "frmRndTree"
  157. Attribute VB_GlobalNameSpace = False
  158. Attribute VB_Creatable = False
  159. Attribute VB_PredeclaredId = True
  160. Attribute VB_Exposed = False
  161. Option Explicit
  162. Private Const PI = 3.14159
  163. ' Recursively draw a tree branch.
  164. Private Sub DrawBranch(ByVal bend As Single, ByVal thickness As Single, ByVal Depth As Integer, ByVal X As Single, ByVal Y As Single, ByVal length As Single, ByVal length_scale As Single, ByVal rnd_scale As Single, ByVal theta As Single, ByVal dtheta As Single, ByVal rnd_dtheta As Single, ByVal max_branches As Integer)
  165. Const DIST_PER_BEND = 5#
  166. Const BEND_FACTOR = 2#
  167. Const MAX_BEND = PI / 6
  168. Dim x1 As Integer
  169. Dim y1 As Integer
  170. Dim x2 As Integer
  171. Dim y2 As Integer
  172. Dim status As Integer
  173. Dim num_bends As Integer
  174. Dim num_branches As Integer
  175. Dim i As Integer
  176. Dim new_length As Integer
  177. Dim new_theta As Single
  178. Dim new_bend As Single
  179. Dim dt As Single
  180. Dim t As Single
  181.     If thickness > 0 Then picCanvas.DrawWidth = thickness
  182.     ' Draw the branch.
  183.     If bend > 0 Then
  184.         ' This is a bending branch.
  185.         num_bends = length / DIST_PER_BEND
  186.         t = theta
  187.         x1 = X
  188.         y1 = Y
  189.         For i = 1 To num_bends
  190.             x2 = x1 + DIST_PER_BEND * Cos(t)
  191.             y2 = y1 + DIST_PER_BEND * Sin(t)
  192.             picCanvas.Line (x1, y1)-(x2, y2)
  193.         
  194.             t = t + bend * (Rnd - 0.5)
  195.             x1 = x2
  196.             y1 = y2
  197.         Next i
  198.     Else
  199.         ' This is a straight branch.
  200.         x1 = X + length * Cos(theta)
  201.         y1 = Y + length * Sin(theta)
  202.         picCanvas.Line (X, Y)-(x1, y1)
  203.     End If
  204.     ' If depth > 1, draw the attached branches.
  205.     If Depth > 1 Then
  206.         num_branches = Int((max_branches - 1) * Rnd + 2)
  207.         dt = 2 * dtheta / (num_branches - 1)
  208.         t = theta - dtheta
  209.         For i = 1 To num_branches
  210.             new_length = length * (length_scale + rnd_scale * (Rnd - 0.5))
  211.             new_theta = t + rnd_dtheta * (Rnd - 0.5)
  212.             t = t + dt
  213.             If bend > 0 Then
  214.                 new_bend = bend * BEND_FACTOR
  215.                 If new_bend > MAX_BEND Then new_bend = MAX_BEND
  216.             Else
  217.                 new_bend = bend
  218.             End If
  219.             DrawBranch new_bend, thickness - 1, _
  220.                 Depth - 1, x1, y1, new_length, _
  221.                 length_scale, rnd_scale, new_theta, _
  222.                 dtheta, rnd_dtheta, max_branches
  223.         Next i
  224.     End If
  225. End Sub
  226. Private Sub CmdGo_Click()
  227. Dim thickness As Integer
  228. Dim bend As Single
  229. Dim Depth As Integer
  230. Dim length As Single
  231. Dim length_scale As Single
  232. Dim rnd_scale As Single
  233. Dim dtheta As Single
  234. Dim rnd_dtheta As Single
  235. Dim max_branches As Integer
  236.     picCanvas.Cls
  237.     MousePointer = vbHourglass
  238.     DoEvents
  239.     ' Get the tree parameters.
  240.     If Not IsNumeric(txtDepth.Text) Then txtDepth.Text = "5"
  241.     Depth = CInt(txtDepth.Text)
  242.     If Not IsNumeric(txtLengthScale.Text) Then txtLengthScale.Text = "0.75"
  243.     length_scale = CSng(txtLengthScale.Text)
  244.     If Not IsNumeric(txtDTheta.Text) Then txtDTheta.Text = "36"
  245.     dtheta = CSng(txtDTheta.Text) * PI / 180#
  246.     If Not IsNumeric(txtRndScale.Text) Then txtRndScale.Text = "0.2"
  247.     rnd_scale = CSng(txtRndScale.Text)
  248.     If Not IsNumeric(txtRndDTheta.Text) Then txtRndDTheta.Text = "20"
  249.     rnd_dtheta = CSng(txtRndDTheta.Text) * PI / 180#
  250.     If Not IsNumeric(txtMaxBranches.Text) Then txtMaxBranches.Text = "3"
  251.     max_branches = CInt(txtMaxBranches.Text)
  252.     If chkTaper.Value = vbChecked Then
  253.         thickness = Depth
  254.     Else
  255.         thickness = 0
  256.     End If
  257.     If chkBend.Value = vbChecked Then
  258.         bend = PI / 90
  259.     Else
  260.         bend = 0
  261.     End If
  262.     length = (picCanvas.ScaleHeight - 10) / _
  263.         ((1 - length_scale ^ (Depth + 1)) / (1 - length_scale))
  264.     ' Draw the tree.
  265.     DrawBranch bend, thickness, Depth, _
  266.         picCanvas.ScaleWidth \ 2, _
  267.         picCanvas.ScaleHeight - 5, _
  268.         length, length_scale, rnd_scale, _
  269.         -PI / 2, dtheta, rnd_dtheta, max_branches
  270.     MousePointer = vbDefault
  271. End Sub
  272. Private Sub Form_Load()
  273.     Randomize
  274. End Sub
  275. Private Sub Form_Resize()
  276. Dim wid As Single
  277.     wid = ScaleWidth - picCanvas.Left
  278.     If wid < 120 Then wid = 120
  279.     picCanvas.Move picCanvas.Left, 0, wid, ScaleHeight
  280. End Sub
  281.